﻿#pragma once

template<class T>
class CProxy_IAtlControlEvents :
	public ATL::IConnectionPointImpl<T, &__uuidof(_IAtlControlEvents)>
{
public:
    HRESULT FireCPTest(BSTR lpbString)
    {
        HRESULT hResult;
        UINT32 nCnt = m_vec.GetSize();

        T* p = static_cast<T*>(this);

        for (UINT32 i = 0; i < nCnt; i++)
        {
            p->Lock();
            CComPtr<IUnknown> pUnk = m_vec.GetAt(i);
            p->Unlock();

            IDispatch* lpIDispatch = static_cast<IDispatch*>(pUnk.p);

            if (lpIDispatch)
            {
                //调用连接点中的函数
                CComVariant avarParams[1];
                avarParams[0] = lpbString;
                avarParams[0].vt = VT_BSTR;

                CComVariant varResult;

                DISPPARAMS DispParams = { avarParams,nullptr,1,0 };
                hResult = lpIDispatch->Invoke(1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD,
                    &DispParams, &varResult, nullptr, nullptr);
            }
        }
        return hResult;
    }
};
